﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UI.BudgetCheck;
using BL.Client.BudgetCheckBL;
using Common.ClassLib;
using UI.BudgetCheck.ProgramData;
using Common.UIFunction;


namespace UI.BudgetCheck.ChildForm.BudgetApprove
{
    public partial class BudgetApprove : Form
    {
        BudgetCheckServerice BCS;
        public BudgetApprove()
        {
            InitializeComponent();
        }

        private void BudgetApprove_Load(object sender, EventArgs e)
        {
            this.BCS = ((BCMain)this.MdiParent).BCS;
            this.InitDGV();
            
        }

        private void InitDGV()
        {
            new DataGridViewBound(this.dgvBudgetApprove).BoundColumn(BudgetQueryData.HeaderText,
             BudgetQueryData.ColumnName, BudgetQueryData.DataPropertyName, BudgetQueryData.Width);
            new DataGridViewBound(this.dgvBudgetItem).BoundColumn(BudgetItemQueryData.HeaderText,
                    BudgetItemQueryData.ColumnName, BudgetItemQueryData.DataPropertyName, BudgetItemQueryData.Width);

             DetailDataGridViewBound uif =
                new DetailDataGridViewBound(
                dgvBudgetApprove, dgvBudgetItem, plBudgetItem, llClose);
            uif.BudgetItemShowBound("查看预算明细", "购买体育器材", "llCheckItemInfo");
            uif.OnShowDetails += new DetailDataGridViewBound.ShowDetails(this.BCS.GetBudgetItemInfo);
           
            DataGridViewButtonColumn approvebutton = new DataGridViewButtonColumn();
            approvebutton.Name = "ApproveButton";
            approvebutton.UseColumnTextForButtonValue = true;
            approvebutton.Text = "批准";
            approvebutton.HeaderText = "预算审核";
            dgvBudgetApprove.Columns.Add(approvebutton);
            
            DataGridViewButtonColumn rejectbutton = new DataGridViewButtonColumn();
            rejectbutton.Name = "RejectButton";
            rejectbutton.UseColumnTextForButtonValue = true;
            rejectbutton.Text = "否决";
            rejectbutton.HeaderText = "预算否决";
            dgvBudgetApprove.Columns.Add(rejectbutton);

            DataGridViewCheckBoxColumn checkboxc = new DataGridViewCheckBoxColumn();
            checkboxc.Name = "cbSelect";
            checkboxc.HeaderText = "选择";
            checkboxc.Width = 35;
            dgvBudgetApprove.Columns.Insert(0, checkboxc);

            dgvBudgetApprove.CellContentClick += new DataGridViewCellEventHandler(dgvFinancialBA_CellContentClick);

            this.ShowBudget();

        }

        

        protected void ShowBudget()
        {
            DataSet ds = this.BCS.GetBudgetInfoByAuth();
            dgvBudgetApprove.DataSource = ds.Tables[0];
        }

        
        protected void dgvFinancialBA_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex.Equals(this.dgvBudgetApprove.Columns["ApproveButton"].Index))
            {
                string budget_approve_needcount =
                    this.dgvBudgetApprove.Rows[e.RowIndex].Cells["BudgetApproveNeedCount"].FormattedValue.ToString();
                string budget_id = this.dgvBudgetApprove.Rows[e.RowIndex].Cells["BudgetId"].Value.ToString().Trim();
                if (!this.BCS.HaveBAAuthority(budget_id))
                {
                    MessageBox.Show("当前用户没有审批权限！");
                    return;
                }
                if (this.BCS.BudgetApprove(budget_id, true, budget_approve_needcount))
                    MessageBox.Show("操作成功！该预算已被批准！");
                else
                    MessageBox.Show("操作失败！请检查网络连接！");
                this.ShowBudget();
            }
            else if (e.ColumnIndex.Equals(this.dgvBudgetApprove.Columns["RejectButton"].Index))
            {
                string budget_approve_needcount =
                    this.dgvBudgetApprove.Rows[e.RowIndex].Cells["BudgetApproveNeedCount"].FormattedValue.ToString();
                string budget_id = this.dgvBudgetApprove.Rows[e.RowIndex].Cells["BudgetId"].Value.ToString().Trim();
                if (!this.BCS.HaveBAAuthority(budget_id))
                {
                    MessageBox.Show("当前用户没有审批权限！");
                    return;
                }
                if (this.BCS.BudgetApprove(budget_id, false, budget_approve_needcount))
                    MessageBox.Show("操作成功！该预算已被否决！");
                else
                    MessageBox.Show("操作失败！请检查网络连接！");
                this.ShowBudget();
            }
            
        }

        private void btnFApproveChecked_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dgvBudgetApprove.Rows)
            {
                if (((DataGridViewCheckBoxCell)dgvr.Cells["cbSelect"]).EditedFormattedValue.Equals(true))
                {
                    string budget_approve_needcount = dgvr.Cells["BudgetApproveNeedCount"].FormattedValue.ToString();
                    string budget_id = dgvr.Cells["BudgetId"].Value.ToString().Trim();
                    if (!this.BCS.HaveBAAuthority(budget_id))
                    {
                        MessageBox.Show("当前用户没有审批权限！");
                        return;
                    }
                    bool bl = this.BCS.BudgetApprove(budget_id, true, budget_approve_needcount);
                    if (!bl)
                    {
                        MessageBox.Show("第 " + dgvr.Index.ToString() + " 行操作失败！请检查网络连接！");
                        this.ShowBudget();
                        return;
                    }

                }
            }
            MessageBox.Show("操作成功！所选预算已被批准！");
            this.ShowBudget();
        }

        private void btnFRejectChecked_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgvr in dgvBudgetApprove.Rows)
            {
                if (((DataGridViewCheckBoxCell)dgvr.Cells["cbSelect"]).EditedFormattedValue.Equals(true))
                {
                    string budget_approve_needcount = dgvr.Cells["BudgetApproveNeedCount"].FormattedValue.ToString();
                    string budget_id = dgvr.Cells["BudgetId"].Value.ToString().Trim();
                    if (!this.BCS.HaveBAAuthority(budget_id))
                    {
                        MessageBox.Show("当前用户没有审批权限！");
                        return;
                    }
                    bool bl = this.BCS.BudgetApprove(budget_id, false, budget_approve_needcount);
                    if (!bl)
                    {
                        MessageBox.Show("第 " + dgvr.Index.ToString() + " 行操作失败！请检查网络连接！");
                        this.ShowBudget();
                        return;
                    }
                }
            }
            MessageBox.Show("操作成功！所选预算已被否决！");
            this.ShowBudget();

        }

        

       
        
       

        
    }
}